﻿using QLKDS.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLKDS.DAL
{
    class AuthorsBookManager
    {
        public IList<AuthorsBookInfo> ListAuthorsBook()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryAuthorsBook = from d in db.AuthorsBooks
                                        select new AuthorsBookInfo { Id = d.Id, BookId = d.BookId, AuthorsId = d.AuthorsId };

                return queryAuthorsBook.ToList();
            }
        }


        public void CreateAuthorsBook(AuthorsBook AuthorsBook)
        {
            using (var db = new QLKDSDataContext())
            {
                db.AuthorsBooks.InsertOnSubmit(AuthorsBook);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="AuthorsBook"></param>
        public void UpdateAuthorsBook(AuthorsBook AuthorsBook)
        {
            using (var db = new QLKDSDataContext())
            {
                var _AuthorsBook = db.AuthorsBooks.Single(d => d.Id.Equals(AuthorsBook.Id));

                _AuthorsBook.BookId = AuthorsBook.BookId;
                _AuthorsBook.AuthorsId = AuthorsBook.AuthorsId;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAuthorsBook(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _AuthorsBook = db.AuthorsBooks.Single(d => d.Id.Equals(id));

                db.AuthorsBooks.DeleteOnSubmit(_AuthorsBook);
                db.SubmitChanges();
            }
        }

    }
}
